<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" 
	xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    
    <!-- TopJUI框架样式 -->
    <link type="text/css" href="/topjui/themes/default/topjui.core.min.css" rel="stylesheet">
    <link type="text/css" href="/topjui/themes/default/topjui.blue.css" rel="stylesheet" id="dynamicTheme"/>
    <!-- FontAwesome字体图标 -->
    <link type="text/css" href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <!-- layui框架样式 -->
    <link type="text/css" href="/static/plugins/layui/css/layui.css" rel="stylesheet"/>
    <!-- iconfont字体图标 -->
	<link type="text/css" href="/static/plugins/iconfont/iconfont.css" rel="stylesheet"/>
    <!-- ztree css -->
	<link rel="stylesheet" href="/static/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
    
    <!-- jQuery相关引用 -->
    <script type="text/javascript" src="/static/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/static/plugins/jquery/jquery.cookie.js"></script>
    <!-- TopJUI框架配置 -->
    <script type="text/javascript" src="/static/public/js/topjui.config.js"></script>
    <!-- TopJUI框架核心-->
    <script type="text/javascript" src="/topjui/js/topjui.core.min.js"></script>
    <!-- TopJUI中文支持 -->
    <script type="text/javascript" src="/topjui/js/locale/topjui.lang.zh_CN.js"></script>
    <!-- layui框架js -->
    <script type="text/javascript" src="/static/plugins/layui/layui.js" charset="utf-8"></script>
    <!-- ztree js -->
	<script type="text/javascript" src="/static/plugins/zTree/js/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="/static/plugins/zTree/js/jquery.ztree.excheck.js"></script>
</head>

<body>
	<div sec:authorize="hasAuthority('system:role')" data-toggle="topjui-layout" data-options="fit:true">
		<div sec:authorize="hasAuthority('system:role:list')" data-options="region:'center',title:'',split:true,border:false,bodyCls:'border_right'">
			<table id="sysRole-list" data-toggle="topjui-datagrid" data-options="
				id:'sysRole-list',url:'/system/role/list',
				fitColumns:true,singleSelect:true,selectOnCheck:false,checkOnSelect:false,pageSize:50">
				<thead>
					<tr>
						<th data-options="field:'id',title:'ID',checkbox:true"></th>
						<th data-options="field:'name',title:'角色名称',width:100"></th>
						<th data-options="field:'remark',title:'备注',width:100"></th>
						<th data-options="field:'createTime',title:'操作时间',width:100"></th>
					</tr>
				</thead>
			</table>
		</div>
		
		<div sec:authorize="hasAuthority('system:role:menu')" id="sysMenu-div" data-options="region:'east',width:'25%',title:'',split:true,border:false,bodyCls:'border_left_right'">
			<!-- 标签栏（角色权限） -->
			<div id="sysMenu-label-bar" style="visibility: hidden;">
				<a href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="disabled:true,iconCls:'fa fa-lock'">角色权限</a>
			</div>
			<!-- 工具栏（角色授权） -->
			<div sec:authorize="hasAuthority('system:role:set_menu')" id="sysMenu-tool-bar" style="visibility: hidden;">
				<a href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="iconCls:'fa fa-pencil'" onclick="setPermissions()" >角色授权</a>
			</div>
			
			<!-- 角色权限-ZTree -->
			<div style="padding-top: 40px;padding-bottom: 10px;">
				<ul id="sysMenu-tree" class="ztree" style="visibility: hidden;"></ul>
			</div>
			<form id="sysMenu-form">
			    <input type="hidden" id="sysRoleId" name="roleId"/>
			    <input type="hidden"  id="sysMenuIds" name="menuIds" />
			</form>
		</div>
	</div>
	
	<!-- 表格工具栏 -->
	<div sec:authorize="hasAuthority('system:role:list')" id="sysRole-list-toolbar" class="topjui-toolbar" data-options="grid:{type:'datagrid', id:'sysRole-list'}">
		<a sec:authorize="hasAuthority('system:role:add')" href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
			method:'openDialog',
			extend:'#sysRole-list-toolbar',
			iconCls:'fa fa-plus',
			dialog:{
  				id:'sysRole-add-dialog',
				title:'新增角色',
				href:'/system/role/add_view',
				width:'60%',
				height:'60%',
				buttonsGroup:[{
					text:'确定',
					url:'/system/role/add',
					iconCls:'fa fa-plus',
					handler:'ajaxForm',
					btnCls:'topjui-btn-green'
				}]
			}">新增</a>
		
		<a sec:authorize="hasAuthority('system:role:edit')" href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
			method:'openDialog',
			extend:'#sysRole-list-toolbar',
			iconCls:'fa fa-pencil',
			dialog:{
				id:'sysRole-edit-dialog',
				title:'编辑角色',
				href: '/system/role/edit_view/{id}',
				width:'60%',
				height:'60%',
				buttonsGroup:[{
					text:'确定',
					url:'/system/role/edit',
					iconCls:'fa fa-plus',
					handler:'ajaxForm',
					btnCls:'topjui-btn-green'
				}]
			}">编辑</a>
		
		<a sec:authorize="hasAuthority('system:role:delete')" href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
			method:'doAjax',
			extend:'#sysRole-list-toolbar',
			iconCls:'fa fa-trash',
			confirmMsg:'确认删除已选择的数据？',
			grid: {uncheckedMsg:'请先勾选要删除的数据',param:'ids:id'},
			url:'/system/role/delete'">删除</a>
		
		<form sec:authorize="hasAuthority('system:role:query')" id="sysRole-queryForm" class="search-box" style="float:right">
			<input type="text" name="name" data-toggle="topjui-textbox" data-options="id:'name',prompt:'名称',width:200">
			<a href="javascript:void(0)" data-toggle="topjui-menubutton" data-options="
				method:'query',
				extend:'#sysRole-list-toolbar',
				iconCls:'fa fa-search',
				form:{id:'sysRole-queryForm'},
				grid:{type:'datagrid','id':'sysRole-list'}">查询</a>
		</form>
	</div>
</body>

<style>
	#sysMenu-label-bar {
		width: 100%;
		padding: 3px 2px;
		border-bottom: 1px solid #ddd;
		background-color: #FFF;
		position: fixed;
	}
	#sysMenu-tool-bar {
		width: 100%;
		padding: 3px 2px;
		border-bottom: 1px solid #ddd;
		background-color: #FFF;
		position: fixed;
	}
	#sysMenu-tree {
		width: 100%;
		height: 100%;
    	border: 0;
    	background: #FFF;
	}
	#sysMenu-tree li a {
		text-decoration: none;
	}
</style>

<script type="text/javascript">
	var setting = {
		check : {
			enable : true
		},
		data : {
			simpleData : {
				enable : true
			}
		}
	};
	
	// 角色列表，选择行事件，初始化菜单树
	$('#sysRole-list').iDatagrid({
		onSelect : function(index, row) {
			if($("#sysMenu-div").length > 0) {
				$("#sysRoleId").val(row.id);
				if($("#sysMenu-tool-bar").length > 0) {
					$("#sysMenu-tool-bar").css('visibility','visible');
				} else {
					$("#sysMenu-label-bar").css('visibility','visible');
				}
				$("#sysMenu-tree").css('visibility','visible');
				$.ajax({
					type : "GET",
					url : "/system/role/menu_tree/" + row.id,
					success : function(result) {
						$.fn.zTree.init($("#sysMenu-tree"), setting, result);
			        }
			    });
			}
		}
	});
	
	// 角色授权
	function setPermissions() {
		$.iMessager.confirm('角色授权','对该角色进行授权？', function(r) {
		    if (r){
		    	var sysMenuIds = [];
				var checkedNodes = $.fn.zTree.getZTreeObj("sysMenu-tree").getCheckedNodes(true); 
				$.each(checkedNodes, function(k, v) {
					sysMenuIds.push(v.id);
		        });
				$("#sysMenuIds").val(sysMenuIds);
				
				$.ajax({
					type : "POST",
					url : "/system/role/set_menu",
					data : $('#sysMenu-form').serialize(),
					success : function(result) {
						if (result.statusCode == 200) {
							$.iMessager.show({
								title : '操作提示',
								msg : '角色授权成功',
								timeout : 1000,
								showType : 'slide'
							});
						} else {
							$.iMessager.show({
								title : '操作提示',
								msg : '角色授权失败，请稍后重试',
								timeout : 1000,
								showType : 'slide'
							});
						}
					}
				});
			}
		});
	}
</script>
</html>